<template>
  <div>
    <el-form
      :inline="true"
      :model="form"
      size="small"
      class="searchForm bg-gray flex flex-wp flex-a-c"
    >
      <el-form-item label="分类名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-button icon="el-icon-search" @click="search()" size="small"
        >查询</el-button
      >
      <el-button size="small" @click="syncData()"
        >同步分类信息</el-button
      >
    </el-form>
    <el-table
      border
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      lazy
      >
      <!-- <el-table-column type="selection" width="40" align="center">
      </el-table-column> -->
      <el-table-column prop="name" label="分类名称" align="center" width="160">
      </el-table-column>
      <el-table-column prop="num" label="下级分类" align="center">
        <template slot-scope="scope">
          <u class="uName" @click="showObj(scope.row)">{{ scope.row.num }}</u>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          {{ scope.row.status == 1 ? "下架" : "上架" }}
        </template></el-table-column
      >
      <el-table-column label="分类排序" align="center">
        <template slot-scope="scope">
          <div class="row">
            <span v-if="!scope.row.isEdit">{{ scope.row.sorting }}</span>
            <el-input size="mini" v-if="scope.row.isEdit" v-model="scope.row.sorting"></el-input>
            <u class="uName">
              <i class="el-icon-edit" v-if="!scope.row.isEdit" @click="scope.row.isEdit = true"></i>
              <i class="el-icon-check" v-if="scope.row.isEdit" @click="checkSort(scope.row)"></i>
            </u>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" width="200">
        <template slot-scope="scope">
          {{ new Date(scope.row.addTime).Format("YYYY-MM-DD hh:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="350"
        class-name="actionTd"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            round
            v-if="scope.row.status === 1"
            @click="editStatus(scope.row.id, 0)"
            type="success"
          >
            上架</el-button
          >
          <el-button
            size="mini"
            round
            type="danger"
            v-if="scope.row.status === 0"
            @click="editStatus(scope.row.id, 1)"
          >
            下架</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="text-c margin-t-d">
      <el-pagination
        @current-change="getDataList"
        :current-page.sync="pageInfo.pageindex"
        :page-size="pageInfo.pagesize"
        background
        layout="total,prev, pager, next, jumper"
        :total="pageInfo.total"
      ></el-pagination>
    </div>
    <el-dialog
      :close-on-click-modal="false"
      :title="obj.name"
      :visible.sync="rateVisible"
    >
      <nextList v-if="rateVisible" :id="obj.id"/>
    </el-dialog>
    
  </div>
</template>
<script>
import nextList from "./nextList.vue";

export default {
  data() {
    return {
      activeName: "1",
      form: {
        name: "",
        parentId:0
      },
      qdForm: {},
      pageInfo: {
        pagesize: 10,
        pageindex: 1,
        total: 0,
      }, //页码
      tableData: [], //表格数据
      multipleSelection: [],
      userCardInfo:{},
      detailVisible: false,
      teamListVisible: false,
      rateVisible: false,
      choiceId: null,
      obj:{}
    };
  },
  components: {
    nextList,
  },
  beforeMount() {
    this.search();
  },
  methods: {
    //多选按钮点击
    handleSelectionChange(data) {
      // console.log(data);
      this.multipleSelection = data;
    },
    showDialog(item, type) {
      this.choiceId = item.userId;
      this.$nextTick(() => {
        if (type === 1) {
          this.teamListVisible = true;
        } else if(type === 2) {
          const loading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          this.$ajax
            .getData("adm/agent/authInfoAndBankInfo", { userId: item.userId })
            .then((data) => {
              loading.close();
              if(data.resData===undefined){
                this.$message.error('信息不存在')
              }
              this.userCardInfo = data.resData || {};
            })
            .catch((error) => {
              loading.close();
              this.$message({
                message: error.codeDes || error.message,
                type: "error",
              });
            });
          this.detailVisible = true;
        } else if(type === 3) {
          this.rateVisible = true
        }
      });
    },
    search() {
      this.qdForm = {
        ...this.form,
      };
      this.pageInfo = {
        pagesize: 10,
        pageindex: 1,
        total: 0,
      }; //页码
      this.getDataList();
    },

    checkSort(obj) {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.$ajax
        .postData("adm/productClassify/sort", {
          id:obj.id,
          sort:obj.sorting,
        })
        .then((data) => {
          loading.close();
          this.$message({
            type: "success",
            message: `修改成功!`,
          });
          this.getDataList();
        })
        .catch((error) => {
          loading.close();
          this.$message({
            message: error.codeDes || error.message,
            type: "error",
          });
        });
    },
    getDataList() {
      let params = {
        pageNum: this.pageInfo.pageindex,
        pageSize: this.pageInfo.pagesize,
        ...this.qdForm,
      };
      this.loading = true;
      this.$ajax
        .getData("adm/productClassify/getList", params)
        .then((data) => {
          this.loading = false;
          data.resData.data.length && data.resData.data.map(_=>{
            _['isEdit'] = false
          })
          this.tableData = data.resData.data;
          this.pageInfo.total = data.resData.count;
        })
        .catch((error) => {
          this.loading = false;
          this.$message({
            message: error.codeDes || error.message,
            type: "error",
          });
        });
    },
    syncData() {
      const loading = this.$loading({
        lock: true,
        text: "正在同步...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.$ajax
        .postData("adm/productClassify/sync")
        .then((data) => {
          loading.close();
          this.search()
          // this.tableData = data.resData.data;
          // this.pageInfo.total = data.resData.count;
        })
        .catch((error) => {
          loading.close();
          this.$message({
            message: error.codeDes || error.message,
            type: "error",
          });
        });
    },
    showObj(obj) {
      this.obj = obj
      this.rateVisible = true
    },
    editStatus(id, status) {
      this.$confirm(`是否确定${status == 1 ? "下架" : "上架"}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          this.$ajax
            .postData("adm/productClassify/upOrDown", {
              id,
              status,
            })
            .then((data) => {
              loading.close();
              this.$message({
                type: "success",
                message: `${status == 1 ? "下架" : "上架"}成功!`,
              });
              this.getDataList();
            })
            .catch((error) => {
              loading.close();
              this.$message({
                message: error.codeDes || error.message,
                type: "error",
              });
            });
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
.uName {
  color: #409EFF;
  cursor: pointer;
}
.row {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>